const Router = require("express").Router(); // 配置后端路由
const Mysql = require("../../mysql/mysql.js");

// 优惠劵管理查找
Router.post("/findCoupon", (req, response) => {
    Mysql(`SELECT * FROM use_coupons`).then((res) => {
        if (res) {
            response.json({ code: 200, msg: "读取成功", data: res });
        } else {
            response.json({ code: 201, msg: "读取失败" });
        }
    });
});
// 优惠劵分类查找
Router.post("/findClassify", (req, response) => {
    Mysql(`SELECT * FROM use_classify`).then((res) => {
        if (res) {
            response.json({ code: 200, msg: "读取成功", data: res });
        } else {
            response.json({ code: 201, msg: "读取失败" });
        }
    });
});

// 优惠劵分类删除某一项
Router.post("/deleteClassify", (req, response) => {
    Mysql(`DELETE FROM use_classify WHERE id=${req.body.id}`).then((res) => {
        if (res) {
            Mysql(`ALTER TABLE use_classify AUTO_INCREMENT = 1`).then((res) => {
                if (res) {
                    Mysql(`SELECT * FROM use_classify`).then((res) => {
                        response.json({
                            code: 200,
                            msg: "读取成功",
                            data: res,
                        });
                    });
                }
            });
        } else {
            response.json({ code: 201, msg: "删除" });
        }
    });
});

// 优惠劵分类增加某一项
Router.post("/addClassify", (req, response) => {
    console.log(req.body.value);
    Mysql(`INSERT INTO use_classify (tit) VALUES (${req.body.value}) `).then(
        (res) => {
            if (res) {
                Mysql(`SELECT * FROM use_classify`).then((res) => {
                    response.json({
                        code: 200,
                        msg: "读取成功",
                        data: res,
                    });
                });
            }
        }
    );
});

// 会员列表查找
Router.post("/findVip", (req, response) => {
    Mysql(`SELECT * FROM vip_useList`).then((res) => {
        if (res) {
            response.json({ code: 200, msg: "读取成功", data: res });
        } else {
            response.json({ code: 201, msg: "读取失败" });
        }
    });
});

module.exports = Router;
